<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
        <script type="text/JavaScript" src="js/validate.js"></script>
        <script type="text/JavaScript">
        <!--
        function ValidateFields() {
	        return ValidateFieldNotEmpty("newCategoryForm", "category", "category name")
	        && ValidateFieldNotEmpty("newCategoryForm", "fee", "fee")
	        && ValidateFieldNumeric("newCategoryForm", "fee", "fee");
        }
        //-->
        </script>
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
    	// Return list of categories
    	function getCategories()
    	{
			$db = new Connect();

			$sql = "SELECT cid, cName, fee FROM Category";
			
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0)
			{
				return;
			}
			else
			{
				$rows = $db->to_array($result);
				return $rows;
			}
    	}
    
		// Perform the category add
		function addCategory() {
			$db = new Connect();
			
			$category = $_POST['category'];
			$fee = $_POST['fee'];
			  
			$sql = "INSERT INTO Category(cName, fee) " .
				   "VALUES('" . $category . "', " . $fee . ")";
			
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Failed category add
			{
				return false;
			}
			else // New category added
			{
				return true;
			}
		}
		
		// Perform the category change
		function changeCategory() {
			$db = new Connect();
			
			$category = $_POST['category'];
			$fee = $_POST['fee'];
			  
			$sql = "UPDATE Category " .
				   "SET cName = '" . $category . "', " .
				   "fee = " . $fee . " " .
				   "WHERE cid = " . $_GET['cid'];
			
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Failed category change
			{
				return false;
			}
			else // Category changed
			{
				return true;
			}
		}
		
		// Perform the category delete
		function removeCategory() {
			$db = new Connect();
			
			$sql = "DELETE FROM Category " .
				   "WHERE cid = " . $_GET['cid'];
			
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Failed category delete
			{
				return false;
			}
			else // Category deleted
			{
				return true;
			}
		}
		
		// Check to see if we received post data and perform appropriate function
		// TODO: Check for empty fields and valid category values
		$addcategory_failed = false;
		if (isset($_POST["add"]))
			$addcategory_failed = !addCategory();
		$changecategory_failed = false;
		if (isset($_POST["change"]))
			$changecategory_failed = !changeCategory();
		$removecategory_failed = false;
		if (isset($_POST["remove"]))
			$removecategory_failed = !removeCategory();
    ?>
    <div id="main">
    	<p class="title">
    		Categories
    	</p>
<?php
	if ($addcategory_failed == true)
		print "<p class=\"error\">FAILED ADDING NEW CATEGORY!</p>";
	if ($changecategory_failed == true)
		print "<p class=\"error\">FAILED CHANGING CATEGORY!</p>";
	if ($removecategory_failed == true)
		print "<p class=\"error\">FAILED REMOVING CATEGORY!</p>";

	$rows = getCategories();
	
	if (isset($rows))
	{
		$rowcount = count($rows);
		for ($i = 0; $i < $rowcount; $i++)
		{
			print "<p>";
			print "<form action=\"admincategories.php?cid=" . $rows[$i]['cid'] . "\" method=\"post\">";
	        print "<input type=\"text\" name=\"category\" value=\"" . $rows[$i]['cName'] . "\">";
	        print " <input type=\"text\" name=\"fee\" size=\"5\"  value=\"" . $rows[$i]['fee'] . "\"> $ &nbsp";
	        print " <input type=\"submit\" class=\"smallButton\" name=\"change\" value=\"Change\"/>";
	        print " <input type=\"submit\" class=\"smallButton\" name=\"remove\" value=\"Remove\"/>";
	        print "</form>";
			print "</p>";
		}
	}
?>	
	    <p>
			<form id="newCategoryForm" action="" onsubmit="return ValidateFields();" method="post">
	            <input type="text" name="category" placeholder="New category">
	            <input type="text" name="fee" size="5"  placeholder="Fee"> $ &nbsp
	            <input type="submit" class="smallButton" name="add" value="Add"/>
	        </form>
	    </p>
    </div>
</body>
</html>
            
